package cn.iocoder.yudao.module.kt.controller.admin.resource;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.kt.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.module.kt.dal.dataobject.jiaocai.JiaocaiDO;
import cn.iocoder.yudao.module.kt.service.resource.ResourceService;

@Tag(name = "管理后台 - 教材分类")
@RestController
@RequestMapping("/kt/resource")
@Validated
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建教材分类")
    @PreAuthorize("@ss.hasPermission('kt:resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody ResourceSaveReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教材分类")
    @PreAuthorize("@ss.hasPermission('kt:resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody ResourceSaveReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教材分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('kt:resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教材分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('kt:resource:query')")
    public CommonResult<ResourceRespVO> getResource(@RequestParam("id") Long id) {
        ResourceDO resource = resourceService.getResource(id);
        return success(BeanUtils.toBean(resource, ResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教材分类分页")
    @PreAuthorize("@ss.hasPermission('kt:resource:query')")
    public CommonResult<PageResult<ResourceRespVO>> getResourcePage(@Valid ResourcePageReqVO pageReqVO) {
        PageResult<ResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教材分类 Excel")
    @PreAuthorize("@ss.hasPermission('kt:resource:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceExcel(@Valid ResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceDO> list = resourceService.getResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教材分类.xls", "数据", ResourceRespVO.class,
                        BeanUtils.toBean(list, ResourceRespVO.class));
    }

    // ==================== 子表（教材资源） ====================

    @GetMapping("/jiaocai/list-by-resource-id")
    @Operation(summary = "获得教材资源列表")
    @Parameter(name = "resourceId", description = "教材分类表id")
    @PreAuthorize("@ss.hasPermission('kt:resource:query')")
    public CommonResult<List<JiaocaiDO>> getJiaocaiListByResourceId(@RequestParam("resourceId") Long resourceId) {
        return success(resourceService.getJiaocaiListByResourceId(resourceId));
    }

}